package com.bsierp.util.tags;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.bsierp.business.pojo.Bank;
import com.bsierp.business.pojo.Employee;
import com.bsierp.util.Constants;
import com.bsierp.util.hibernate.HibernateSessionFactory;

public class DisplayBankHtmlSelect extends TagSupport {
	
	 String id;
		public String getId() {
			  return id;
			 }
			 public void setId(String id) {
			  this.id = id;
			 }
			 String usetype;
			 
				public String getUsetype() {
				return usetype;
			}
			public void setUsetype(String usetype) {
				this.usetype = usetype;
			}

private Log log = LogFactory.getLog(this.getClass().getName());
   
public int doEndTag( ) throws JspException
{
	
    JspWriter out = pageContext.getOut();
    
    String hql="from Bank";
 
try{
	HttpSession ses = pageContext.getSession();
	boolean isedit=false;
	Employee em = null;
	if(this.getUsetype().equals("edit")&&ses.getAttribute(Constants.employee_sessionindex)!=null){
		isedit=true;
	}
	if(isedit==true){
	em=(Employee)ses.getAttribute(Constants.employee_sessionindex);
	}
	
	
	SessionFactory sessionFactory= HibernateSessionFactory.getSessionFactory();
	Session session=sessionFactory.openSession();
	Transaction tx=null;
	String option_str="";
		try {
		tx=session.beginTransaction();
		List l=session.createQuery(hql).list();
		
		for(int i=0;i<l.size();i++){
			Bank pc=(Bank)l.get(i);
			
			if(isedit==true){
				   if(em.getBank().getBankId().equals(pc.getBankId())){
				      option_str=option_str+"<option selected=\"selected\" value=\""+pc.getBankId()+"\">"+pc.getBankName()+"</option>"   ;
				   }else{
					     option_str=option_str+"<option value=\""+pc.getBankId()+"\">"+pc.getBankName()+"</option>"   ;
		 			   }
				   
				   }else{
					   
					   option_str=option_str+"<option value=\""+pc.getBankId()+"\">"+pc.getBankName()+"</option>"   ;
			 			  
				   }

		
		
		
		}
		
		}catch(Exception e){
			e.printStackTrace();
			
		}finally{
			session.close();
		}
		
		
    out.println("<select name=\"select\" id=\""+this.getId()+"\">" );  
    if(isedit==false){
        out.println("<option value=\"0\" selected=\"selected\">请选择 </option>");
        }  
    out.println(option_str);
    out.println("</select>");
	
}catch (Exception ex)
    {
      throw new JspTagException("IOException:" + ex.toString());
    }
    return super.doEndTag();
}
}
